When magic meets multicore

OCaml and its elegant era of parallelism


Content

  • Merlin, OCaml's language server.
  • A few performance issues.
  • OCaml multicore for performance improvements.

Structure

The talk will be structured into three parts:

1. Merlin 🧙‍♀️

What is Merlin?

Once apon a time, there was a wizard that introduced editor support to OCaml.
  • Merlin is the backend for OCaml's language server.

  • It brings editor support to OCaml.

What is editor support?

  • (imagine a live demo)

What is a language server?

  • (imagine a simple visualization)

How does a language server compute its information?

pipeline

2. Multicore OCaml

We'll explain two concepts:

A domain in OCaml is a parallel execution unit that has its own minor heap, and execution stack. Domains allow OCaml programs to run code in parallel on multiple CPU cores without a global runtime lock.

(imagine a visualization)

An effect is an operation in a program that can suspend the linear flow of execution and delegate control to an external handler to decide how to proceed.

An effect handler captures and defines the behavior of effects when they are performed. It has access to the continuation (the rest of the computation).

(imagine code snippet building up)

3. Multicore Merlin

By now, we've explained two things:

  • OCaml multicore
  • Merlin 🧙‍♀️
    • friendly wizard: everybody loves Merlin
    • old wizard: 10+ years legacy code base with highly sequential logic and lots of global mutable state

It sounded like a horrible idea to use the new multicore paradigm on Merlin.

So, we did it anyways! :)

  • The type-checking logic is run on a separate domain from the computations.
  • Effect handlers allow to have an easy-to-handle control flow
  • Lock spells are used to manage shared state
  • We used a tool called TSan to dynamically detect data races
  • Queries are now cancellable
  • Computations can be started on top of partial typing results
  • The new model opens the door to more performance improvements

Spoiler: It has worked surprisingly well!

Bonus: We'll point out interesting aspects of OCaml as we go.

0